<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="年度" prop="year">
        <el-date-picker
          v-model="queryParams.year"
          type="year"
          format="yyyy"
          value-format="yyyy"
          placeholder="选择种植年度">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="供水断面" prop="crossCd" width="50px">
          <!-- <treeselect
              v-model="queryParams.crossCd"
              :options="chanList"
              :normalizer="normalizer"
              :show-count="true"
              :clearable="false"
              placeholder="进水断面"
          /> -->
          <el-select v-model="queryParams.crossCd">
              <el-option
                  v-for="item in chanList"
                  :key="item.crossCd"
                  :label="item.crossNm"
                  :value="item.crossCd"
              ></el-option>
          </el-select>
      </el-form-item>
     <el-form-item>
       <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
<!--          <el-button-->
<!--              type="warning"-->
<!--              plain-->
<!--              icon="el-icon-download"-->
<!--              size="mini"-->
<!--              @click="handleExport"-->
<!--              v-hasPermi="['waterDistribution:wrDistMonCmp:export']"-->
<!--          >导出</el-button>-->
     </el-form-item>
    </el-form>

    <div class="tb_title">渠系供水过程管理</div>
    <div class="tb_dw" >水量：<span class="hj_cl_blue">m³</span></div>

    <el-table v-loading="loading" :data="wrDistMonCmpList" stripe :header-cell-style="tableHeaderColor">
      <el-table-column label="月份" fixed="left" align="center" prop="getYm" show-overflow-tooltip/>
      <el-table-column label="上旬" align="center" prop="early" show-overflow-tooltip>
        <el-table-column label="定额水量"  align="center" prop="firstCtrl" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="修订水量"  align="center" prop="firstGet" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-show="!scope.row.xEdit">{{ scope.row.firstGet == '0.00'?'':scope.row.firstGet }}</span>
            <el-input v-show="scope.row.xEdit" size="mini" v-model="scope.row.firstGet"></el-input>
          </template>
        </el-table-column>
        
      </el-table-column>
      <el-table-column label="中旬" align="center" prop="middle" show-overflow-tooltip>
        <el-table-column label="定额水量"  align="center" prop="secondCtrl" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="修订水量"  align="center" prop="secondGet" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-show="!scope.row.xEdit">{{ scope.row.secondGet == '0.00'?'':scope.row.secondGet }}</span>
            <el-input v-show="scope.row.xEdit" size="mini" v-model="scope.row.secondGet"></el-input>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="下旬" align="center" prop="late" show-overflow-tooltip>
        <el-table-column label="定额水量"  align="center" prop="thirdCtrl" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="修订水量"  align="center" prop="thirdGet" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-show="!scope.row.xEdit">{{ scope.row.thirdGet == '0.00'?'':scope.row.thirdGet }}</span>
            <el-input v-show="scope.row.xEdit" size="mini" v-model="scope.row.thirdGet"></el-input>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="月水量" align="center" prop="volume" show-overflow-tooltip>
        <el-table-column label="定额水量"  align="center" prop="monCtrl" show-overflow-tooltip>

        </el-table-column>
        <el-table-column label="修订水量"  align="center" prop="monGet" show-overflow-tooltip>
          <template slot-scope="scope">
            <span >{{ parseFloat(scope.row.firstGet)  + parseFloat(scope.row.secondGet) +  parseFloat(scope.row.thirdGet)}}</span>
          </template>
        </el-table-column>
        <template slot-scope="scope">
          <span >{{ scope.row.volume == '0.00'?'':scope.row.volume}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100px" >
          <template slot-scope="scope">
              <el-button v-show="scope.$index!=12&&!scope.row.xEdit" size="mini" type="text" icon="el-icon-edit"  @click="handleUpdate(scope.$index, scope.row)"
              >编辑</el-button>
              <el-button v-show="scope.$index!=12&&scope.row.xEdit" size="mini" type="text"  @click="handlesave(scope.$index, scope.row)"
              >保存</el-button>
              <el-button v-show="scope.$index!=12&&scope.row.xEdit" size="mini" type="text"  @click="handleQuit(scope.$index, scope.row)"
              >撤销</el-button>
          </template>
      </el-table-column>
    </el-table>
    <el-row type="flex" justify="end" style="margin:20px">
        <el-button type="primary"  size="mini" @click="handleClick(1)">导入上年数据</el-button>
        <el-button type="primary"  size="mini" @click="handleClick(2)">重新计算</el-button>
    </el-row>
  </div>
</template>

<script>
import { setCroVol,wiGetvolMList,importLastYearData , getChanAdAreaRegroup , updateWiGetvolM } from "@/api/waterDistribution/wrSupplyManage";
import {
  listStCrossB
} from "@/api/datam/StCrossB";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "WrDistMonCmp",
  components: { Treeselect },
  data() {
    return {
      chanList:[],
      // 行政区划树选项
      xzqhOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 年份选择
      yearList: [],
      // 用水对比表格数据
      wrDistMonCmpList: [],
      // 弹出层标题
      title: "",
      // 列表标题行内容
      tableTitle: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        // pageNum: 1,
        // pageSize: 10,
        year: null,
        crossCd:'',
      },
    };
  },
  created() {
    this.listChan()
  },
  methods: {
    /** 查询渠（沟）道断面基础信息列表 */
    listChan() {
      listStCrossB({crossType:2}).then((response) => {
        this.chanList = response.rows;
        this.queryParams.crossCd = response.rows[0].crossCd
        this.getList();
      });
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
        if (node.children && !node.children.length) {
            delete node.children;
        }
        return {
            id: node.crossCd,
            label: node.chanName,
            children: node.children,
        };
    },
    /** 查询行政区域下拉树结构 */
    getTreeselect() {
      getxzqh().then((response) => {
        this.xzqhOptions = this.handleTree(response.data, "crossCd", "upcrossCd");
      });
    },

    /** 查询用水对比列表 */
    getList() {
      this.loading = true;
      if(this.queryParams.year == null) {
        var day = new Date();
        var nowYear = day.getFullYear().toString();
        this.queryParams.year = nowYear;
      }
      this.loading = false;
      this.tableTitle = "富蕴县" + this.queryParams.year + "年度灌区限额用水执行情况表";
      wiGetvolMList(this.queryParams).then(response => {
        let arr = response.rows.map((element,index) => {
          return {
            ...element,
            xEdit:false,
          }
        });
        this.wrDistMonCmpList = arr;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //编辑功能
    handleUpdate(index, row){
      let isck = false;
      this.wrDistMonCmpList.forEach((item, index) => {
        if(item.xEdit){
          isck = item.xEdit;
          return;
        }
      });
      if(isck){
        this.$message({
          showClose: true,
          message: '请保存已编辑内容！！！',
          type: 'error'
        });
        return;
      }

      this.wrDistMonCmpList = this.wrDistMonCmpList.map((item, i) => {
        if (index === i) {
          item.xEdit = true;
        }
        return item
      })
    },
    //编辑保存
    handlesave(index, row){
      if(row.middle == '' || row.early == '' || row.late == ''){
         this.$message({
            type: 'warning',
            message: '请填写数据!'
          });
          return
      }
      let param = {
        ...row,
        year:this.queryParams.year,
        crossCd:this.queryParams.crossCd,
      }
      param.monGet = parseFloat(param.firstGet)  + parseFloat(param.secondGet) +  parseFloat(param.thirdGet)
      updateWiGetvolM(param).then(response => {
        this.$message({
          type: 'success',
          message: '保存成功!'
        });
        this.getList();
      });
    },
    // 撤销功能
    handleQuit(index, row) {
      this.wrDistMonCmpList = this.wrDistMonCmpList.map((item, i) => {
        if (index === i) {
          item.xEdit = false;
        }
        return item
      })
      this.getList();
    },
    handleClick(type){
      if(type == 1){
        this.$confirm('此操作将导入上一年数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          importLastYearData({
            year:this.queryParams.year - 1,
            crossCd:this.queryParams.crossCd,
          }).then(response => {
            this.getList();
            this.$message({
              type: 'success',
              message: '导入成功!'
            });
          })

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消导入'
          });
        });

      }else if(type == 2){
        this.$confirm('此操作将重新计算当前数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          setCroVol({
            year:this.queryParams.year,
            crossCd:this.queryParams.crossCd,
          }).then(response => {
            this.getList();
            this.$message({
              type: 'success',
              message: '重新计算成功!'
            });
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取重新计算'
          });
        });
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('distribute/distribute/wrDistMonCmp/export', {
        ...this.queryParams
      }, `富蕴县` + this.queryParams.year + `年度灌区限额用水执行情况表.xlsx`)
    },
    /** 修改table header的背景色 */
    tableHeaderColor(row, rowIndex) {
      return 'background-color: #e6f4f8; font-weight: 700px !important;';
    },
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

    .tb_title{
        width: 430px;
        text-align: center;
        margin: 5px auto;
        margin-bottom: 15px;
        font-weight: 700;
    }

    .tb_dw{
        font-size: 13px;
        position: absolute;
        top: 84px;
        right: 40px;
    }

    .hj_cl{
        font-weight: 700;
    }

    .hj_cl_blue {
        color: #1890ff !important;
        font-weight: 700;
    }

    .el-table__footer-wrapper tbody td.el-table__cell{
        font-weight: 700;
    }
    .el-table .el-table__footer-wrapper .cell{
        font-weight: 700;
    }

    .vue-treeselect__placeholder, .vue-treeselect__single-value {
        line-height: 30px;
    }

    .vue-treeselect {
        width: 215px;
        height: 30px;
        .vue-treeselect__control {
            height: 30px;
            .el-form-item--medium .el-form-item__content {
                line-height: 30px;
                .vue-treeselect__placeholder, .vue-treeselect__single-value {
                    line-height: 30px;
                }
            }
        }
    }

</style>
